﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Nox.Amuse.ViewModel;

namespace Nox.Amuse.View
{
    /// <summary>
    /// Interaction logic for Tab.xaml
    /// </summary>
    public partial class Tab : UserControl
    {
        public Tab()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Tab_Loaded);

            App.Messenger.Register<TabViewModel>(Messages.TabChanged, (tvm) => { GrabFocus(); });
            App.Messenger.Register(Messages.MainWindowActivated, () => { GrabFocus(); });
            this.outputBuffer.TextInput += new TextCompositionEventHandler(outputBuffer_TextInput);

            this.DataContextChanged += new DependencyPropertyChangedEventHandler(InputBox_DataContextChanged);
        }

        void outputBuffer_TextInput(object sender, TextCompositionEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Text)) // User tried to type something
            {
                int caret = inputBox.CaretIndex;
                inputBox.Text = inputBox.Text.Insert(inputBox.CaretIndex, e.Text);
                GrabFocus();
                inputBox.CaretIndex = caret + e.Text.Length;
            }
            else if (!string.IsNullOrEmpty(e.ControlText))
            {
                if
                ((byte)e.ControlText[0] == 22) // User tried to paste something
                {
                    GrabFocus();
                    inputBox.Paste();
                }
            }
            // Do nothing special if user tried something else
        }

        void Tab_Loaded(object sender, RoutedEventArgs e)
        {
            inputBox.Focus();
        }

        void GrabFocus()
        {
            inputBox.Focus();
        }

        /// <summary>
        /// Stores a local reference to the ViewModel
        /// </summary>
        TabViewModel ViewModel;

        CommandBinding sendCommandBinding;
        InputBinding sendInputBinding;

        void InputBox_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (ViewModel != null)
            {
                inputBox.CommandBindings.Remove(sendCommandBinding);
                inputBox.InputBindings.Remove(sendInputBinding);
            }
            ViewModel = (TabViewModel)e.NewValue;
            if (ViewModel != null)
            {
                sendCommandBinding = new CommandBinding(ViewModel.Send, ViewModel.SendExecute, ViewModel.CanSendExecute);
                sendInputBinding = new InputBinding(ViewModel.Send, new KeyGesture(Key.Enter));

                inputBox.CommandBindings.Add(sendCommandBinding);
                inputBox.InputBindings.Add(sendInputBinding);
            }
        }

        private void TextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            outputBuffer.ScrollViewer.ScrollToBottom();
        }

    }
}